﻿using Coldairarrow.Entity.MEGA;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.MEGA
{
    public class MEGA_LeafBladeAndFactoryBusiness : BaseBusiness<MEGA_LeafBladeAndFactory>, IMEGA_LeafBladeAndFactoryBusiness, ITransientDependency
    {
        public MEGA_LeafBladeAndFactoryBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<MEGA_LeafBladeAndFactoryDTO>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<MEGA_LeafBladeAndFactory>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<MEGA_LeafBladeAndFactory, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }
            q = q.Where(where);
            var qq = from a in q
                     join b in Db.GetIQueryable<MEGA_FactoryManagement>() on a.FId equals b.Id
                     select new MEGA_LeafBladeAndFactoryDTO
                     {
                         Address = b.Address,
                         AreaId = b.AreaId,
                         AreaIds = b.AreaIds,
                         AreaName = b.AreaName,
                         Code = b.Code,
                         CreateTime = a.CreateTime,
                         CreatorId = a.CreatorId,
                         Describe = b.Describe,
                         FId = a.FId,
                         Id = a.Id,
                         Image = b.Image,
                         LBId = a.LBId,
                         Name = b.Name,
                         SmallName = b.SmallName,
                         Note = b.Note
                     };
            return await qq.GetPageResultAsync(input);
        }

        public async Task<MEGA_LeafBladeAndFactory> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(MEGA_LeafBladeAndFactory data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(MEGA_LeafBladeAndFactory data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}